/* Copyright Andy Mitchell 2007. No warranties */

function ThreadActionsUI(gtdinboxPage, elParent){
	this._gtdinboxPage = gtdinboxPage;
	this._doc = gtdinboxPage.doc;
	this._labels = this._gtdinboxPage._labels;
	
	this._eventObservers = new EventObservers(this);
	this._eventHandlers = new EventHandlers_Mutator(this);
	
	this._create(elParent);
};

ThreadActionsUI.prototype._create = function(elParent){
	this.el = this._doc.createElement("DIV");
		this.el.className = "thread-actions";
	if( elParent ) elParent.appendChild( this.el );
	
	var elMarkRead = this._doc.createElement("BUTTON");
		elMarkRead.innerHTML = "Mark as Read";	// TODO language
		this._eventObservers.observe(elMarkRead, "click", this._clickMarkRead, false);
	this.el.appendChild(elMarkRead);
	
	var elArchive = this._doc.createElement("BUTTON");
		elArchive.innerHTML = "Archive";	// TODO language
		this._eventObservers.observe(elArchive, "click", this._clickArchive, false);
	this.el.appendChild(elArchive);
		
	var elDelete = this._doc.createElement("BUTTON");
		elDelete.innerHTML = "Delete";	// TODO language
		this._eventObservers.observe(elDelete, "click", this._clickDelete, false);
	this.el.appendChild(elDelete);
};

ThreadActionsUI.prototype.refresh = function(thread){
	this.thread = thread;
};

ThreadActionsUI.prototype.getFinishedButton = function(){
	var elFinish = this._doc.createElement("BUTTON");
		elFinish.innerHTML = "Finish";	// TODO language
		elFinish.title = "Remove status, ensure marked as read, and archive it";	// TODO language
		this._eventObservers.observe(elFinish, "click", this._clickFinish, false);
	return elFinish;
};


ThreadActionsUI.prototype._clickMarkRead = function(event){
try{
	var f = GIPROTOTYPE.bind(this, function(el){
		if( el.getAttribute && el.getAttribute("act")=="1" ){
			GIPROTOTYPE.doMouseEvent(this._doc, el, "mouseup");
			setTimeout( GIPROTOTYPE.bind(this, function(){	
					this._eventHandlers.broadcast("click-finished");
				}), 500);
			return true;
		}
	});
	this._labels._doMoreAction(f);
}catch(e){
	if( GTDDEBUGMODE ) alert("threadactionsui clickMarkRead\n" + e.toString());
}
};
ThreadActionsUI.prototype._clickArchive = function(event){
try{
	var el = this._gtdinboxPage.gmailDOM.element("archive-button");
	if( el ) GIPROTOTYPE.doMouseEvent(this._doc, el, "click");
	this._eventHandlers.broadcast("click-finished");
}catch(e){
	if( GTDDEBUGMODE ) alert("threadactionsui clickArchive\n" + e.toString());
}
};
ThreadActionsUI.prototype._clickDelete = function(event){
try{
	var el = this._gtdinboxPage.gmailDOM.element("delete-button");
	if( el ){
		GIPROTOTYPE.doMouseEvent(this._doc, el, "click");
		this._eventHandlers.broadcast("click-finished");
	}else{	// no delete button, could be Sent Mail?
		var f = GIPROTOTYPE.bind(this, function(el){
			if( el.getAttribute && el.getAttribute("act")=="10" ){
				GIPROTOTYPE.doMouseEvent(this._doc, el, "mouseup");
				setTimeout( GIPROTOTYPE.bind(this, function(){
						//this._eventHandlers.broadcast("click-finished");
					}), 500);
				return true;
			}
		});
		this._labels._doMoreAction(f);
	}	
}catch(e){
	if( GTDDEBUGMODE ) alert("threadactionsui clickDelete\n" + e.toString());
}
};
ThreadActionsUI.prototype._clickFinish = function(event){
try{
	// Remove all statuses; mark as read; archive
	var statusPrefix = this._gtdinboxPage.gtdinboxBrowser.settings.labels.types["Statuses"].prefix;
	var labels = this.thread.labels;
	var labelled = {'count':0};
	for( var i = 0; i < labels.length; i++ ){
		if( labels[i].indexOf(statusPrefix)==0 ){
			var fCallback = GIPROTOTYPE.bind(this, function(act){
				if( --labelled.count==0 ){	// move on
					this._clickMarkRead();
					// TODO improve by monitoring alert-box to see when mark-read is done
					setTimeout( GIPROTOTYPE.bind(this, function(){
							this._clickArchive();
						}), 500 );
				}
			});
			labelled.count++;
			this._labels.labelThread({labelname:labels[i], 'on':false, 'threads':this.thread, 'fCallback':fCallback});
		}
	}
}catch(e){
	if( GTDDEBUGMODE ) alert("threadactionsui clickFinish\n" + e.toString());
}
};




ThreadActionsUI.prototype.destroy = function(){
	this._eventHandlers.destroy();
	this._eventObservers.destroy();
};